package com.kime.shiro.controller;

import com.kime.shiro.entity.User;
import com.kime.shiro.service.UserService;
import com.kime.shiro.utils.VerifyCodeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: JiangYi
 * @Date: 2022-03-14 23:10:22
 * @Description: 用户模块
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取验证码
     */
    @RequestMapping("image")
    public void getImage(HttpSession session, HttpServletResponse httpServletResponse) throws IOException {
        // 生成验证码
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        // 验证码放入 session
        session.setAttribute("code", verifyCode);
        // 验证码存入图片
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        httpServletResponse.setContentType("image/png");
        VerifyCodeUtils.outputImage(220, 60, outputStream, verifyCode);

    }

    /**
     * 用户注册
     */
    @RequestMapping("/register")
    public String register(User user) {
        try {
            userService.register(user);
            return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/register.jsp";
        }
    }

    /**
     * 退出登录
     */
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout(); // 退出用户
        return "redirect:/login.jsp";
    }

    /**
     * 用来处理身份认证
     */
    @RequestMapping("/login")
    public String login(String username, String password, String code, HttpSession session, String rememberMe) {
        // 比较验证码
        String verifyCode = (String) session.getAttribute("code");
        try {
            if (verifyCode.equalsIgnoreCase(code)) {
                // 获取主体对象
                Subject subject = SecurityUtils.getSubject();
                subject.login(new UsernamePasswordToken(username, password, rememberMe));
                return "redirect:/index.jsp";
            } else {
                throw new RuntimeException("验证码错误～");
            }
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("认证失败: 用户名错误～ ");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("认证失败: 密码错误～");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("认证失败: 其它异常～");
        }
        return "redirect:/login.jsp";
    }
}
